﻿Imports System.ComponentModel
Imports System.Linq.Expressions
Imports System.Text.RegularExpressions

Public Class VM_PublisherInformation
	Inherits AutomaticNotfiyPropertyChanged
	Implements IDisposable

	Private Shared lastCalTime As DateTime

	Dim workingPublisher As PublisherData
	Dim db As New CongregationDataContainerLocal

	Public Property FullName As String
		Get
			Return workingPublisher.FullName
		End Get
		Set(value As String)
			workingPublisher.FullName = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public ReadOnly Property PublisherID As Long
		Get
			Return workingPublisher.PublisherID
		End Get
	End Property

	Public Property IsSpecialPioneer() As Boolean
		Get
			Return workingPublisher.IsSpecialPioneer
		End Get
		Set(ByVal value As Boolean)
			workingPublisher.IsSpecialPioneer = value

			If IsSpecialPioneer Then
				workingPublisher.IsPioneer = False
				workingPublisher.IsRegAuxPioneer = False
				NotifyPropertyChanged(Me, Function(x) x.IsPioneer)
				NotifyPropertyChanged(Me, Function(x) x.IsRegAuxPioneer)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsPioneer As Boolean
		Get
			Return workingPublisher.IsPioneer
		End Get
		Set(value As Boolean)
			workingPublisher.IsPioneer = value

			If IsPioneer Then
				workingPublisher.IsSpecialPioneer = False
				workingPublisher.IsRegAuxPioneer = False
				NotifyPropertyChanged(Me, Function(x) x.IsSpecialPioneer)
				NotifyPropertyChanged(Me, Function(x) x.IsRegAuxPioneer)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsRegAuxPioneer As Boolean
		Get
			Return workingPublisher.IsRegAuxPioneer
		End Get
		Set(value As Boolean)
			workingPublisher.IsRegAuxPioneer = value

			If IsRegAuxPioneer Then
				workingPublisher.IsSpecialPioneer = False
				workingPublisher.IsPioneer = False
				NotifyPropertyChanged(Me, Function(x) x.IsSpecialPioneer)
				NotifyPropertyChanged(Me, Function(x) x.IsPioneer)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsPublisher As Boolean
		Get
			Return Not (IsRegAuxPioneer Or IsPioneer Or IsSpecialPioneer)
		End Get
		Set(value As Boolean)
			If value Then
				workingPublisher.IsSpecialPioneer = False
				workingPublisher.IsPioneer = False
				workingPublisher.IsRegAuxPioneer = False
				NotifyPropertyChanged(Me, Function(x) x.IsSpecialPioneer)
				NotifyPropertyChanged(Me, Function(x) x.IsPioneer)
				NotifyPropertyChanged(Me, Function(x) x.IsRegAuxPioneer)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsMale As Boolean
		Get
			Return workingPublisher.IsMale
		End Get
		Set(value As Boolean)
			workingPublisher.IsMale = value

			If IsMale Then
				workingPublisher.IsElder = False
				workingPublisher.IsMinisterialServant = False
				NotifyPropertyChanged(Me, Function(x) x.IsElder)
				NotifyPropertyChanged(Me, Function(x) x.IsMinisterialServant)
				NotifyPropertyChanged(Me, Function(x) x.IsFemale)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsFemale As Boolean
		Get
			Return Not workingPublisher.IsMale
		End Get
		Set(value As Boolean)
			workingPublisher.IsMale = Not value

			If IsFemale Then
				workingPublisher.IsElder = False
				workingPublisher.IsMinisterialServant = False
				NotifyPropertyChanged(Me, Function(x) x.IsElder)
				NotifyPropertyChanged(Me, Function(x) x.IsMinisterialServant)
				NotifyPropertyChanged(Me, Function(x) x.IsMale)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsMinisterialServant As Boolean
		Get
			Return workingPublisher.IsMinisterialServant
		End Get
		Set(value As Boolean)
			workingPublisher.IsMinisterialServant = value

			If IsMinisterialServant Then
				workingPublisher.IsMale = True
				workingPublisher.IsElder = False
				NotifyPropertyChanged(Me, Function(x) x.IsMale)
				NotifyPropertyChanged(Me, Function(x) x.IsFemale)
				NotifyPropertyChanged(Me, Function(x) x.IsElder)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsElder As Boolean
		Get
			Return workingPublisher.IsElder
		End Get
		Set(value As Boolean)
			workingPublisher.IsElder = value

			If IsElder Then
				workingPublisher.IsMale = True
				workingPublisher.IsMinisterialServant = False
				NotifyPropertyChanged(Me, Function(x) x.IsMale)
				NotifyPropertyChanged(Me, Function(x) x.IsFemale)
				NotifyPropertyChanged(Me, Function(x) x.IsMinisterialServant)
			End If

			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsAnointed As Boolean
		Get
			Return workingPublisher.IsAnointed
		End Get
		Set(value As Boolean)
			workingPublisher.IsAnointed = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsFamilyHead As Boolean
		Get
			Return workingPublisher.IsFamilyHead
		End Get
		Set(value As Boolean)
			workingPublisher.IsFamilyHead = value
			If IsFamilyHead Then
				workingPublisher.FamilyHeadID = workingPublisher.PublisherID
			Else
				FamilyHeadID = Nothing
			End If
			db.SaveChanges()
			NotifyPropertyChanged()
			NotifyPropertyChanged(Me, Function(x) x.FamilyHeads)
			NotifyPropertyChanged(Me, Function(x) x.FamilyHeadID)
		End Set
	End Property

	Public Property IsDeaf As Boolean
		Get
			Return workingPublisher.IsDeaf
		End Get
		Set(value As Boolean)
			workingPublisher.IsDeaf = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property IsBlind As Boolean
		Get
			Return workingPublisher.IsBlind
		End Get
		Set(value As Boolean)
			workingPublisher.IsBlind = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property FamilyHeadID As Long?
		Get
			Return workingPublisher.FamilyHeadID
		End Get
		Set(value As Long?)
			If value IsNot Nothing Then
				workingPublisher.FamilyHeadID = value
				db.SaveChanges()
				NotifyPropertyChanged()
			End If
		End Set
	End Property

	Private Shared _AllFamilyHeads As List(Of VM_PublisherInformation)
	Public Shared ReadOnly Property AllFamilyHeads As List(Of VM_PublisherInformation)
		Get
			If _AllFamilyHeads Is Nothing OrElse (Now.AddSeconds(-1)) > lastCalTime Then
				_AllFamilyHeads = PublisherData._FamilyHeads.OrderBy(Function(x) x.FullName).Select(Function(x) New VM_PublisherInformation(x.PublisherID)).ToList
				lastCalTime = Now
			End If

			Return _AllFamilyHeads
		End Get
	End Property

	Public ReadOnly Property FamilyHeads As List(Of VM_PublisherInformation)
		Get
			Return AllFamilyHeads
		End Get
	End Property

	Public ReadOnly Property FamilyMembers As List(Of VM_PublisherInformation)
		Get
			Dim returnList As New List(Of VM_PublisherInformation)
			If IsFamilyHead Then
				For Each publisher In db.PublisherDatas.Where(Function(x) x.FamilyHeadID = workingPublisher.PublisherID)
					returnList.Add(New VM_PublisherInformation(publisher.PublisherID))
				Next
			End If
			Return returnList
		End Get
	End Property

	Public ReadOnly Property EmergencyContactInformation As VM_EmergencyContactInformation
		Get
			Return New VM_EmergencyContactInformation(FamilyHeadID)
		End Get
	End Property

	Public Property MobileTelephone As String
		Get
			Return workingPublisher.MobileTelephone
		End Get
		Set(value As String)
			workingPublisher.MobileTelephone = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property MobileProviderID As Long?
		Get
			Return workingPublisher.MobileProviderID
		End Get
		Set(value As Long?)
			'make sure it is a valid domain ID
			If value Is Nothing OrElse db.MobileProviders.Where(Function(x) x.ProviderID = value).Count > 0 Then
				workingPublisher.MobileProviderID = value
				db.SaveChanges()
				NotifyPropertyChanged()
			End If
		End Set
	End Property

	Public ReadOnly Property MobileTelephoneProviderName As String
		Get
			If workingPublisher.MobileProvider IsNot Nothing Then
				Return workingPublisher.MobileProvider.ServiceProvider
			Else
				Return ""
			End If
		End Get
	End Property

	Public ReadOnly Property TextMessageEmail As String
		Get
			'strip out all non-numeric data for the email address
			If workingPublisher.MobileProviderID IsNot Nothing AndAlso workingPublisher.MobileTelephone IsNot Nothing Then
				Return String.Format("{0}@{1}", Regex.Replace(workingPublisher.MobileTelephone, "[^0-9]", ""), workingPublisher.MobileProvider.ProviderTextMessageEmailDomain)
			Else
				Return Nothing
			End If
		End Get
	End Property

	Public Property Email As String
		Get
			Return workingPublisher.Email
		End Get
		Set(value As String)
			workingPublisher.Email = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property BirthDate As DateTime?
		Get
			Return workingPublisher.BirthDate
		End Get
		Set(value As DateTime?)
			If BirthDate IsNot Nothing Then
				workingPublisher.BirthDate = CType(value, Date).Date
				db.SaveChanges()
				NotifyPropertyChanged()
			End If
		End Set
	End Property

	Public Property ImmersionDate As DateTime?
		Get
			Return workingPublisher.DateImmersed
		End Get
		Set(value As DateTime?)
			If value IsNot Nothing Then
				workingPublisher.DateImmersed = CType(value, Date).Date
				db.SaveChanges()
				NotifyPropertyChanged()
			End If
		End Set
	End Property

	Public Property BeganPublishing As DateTime
		Get
			Return workingPublisher.BeganPublishing
		End Get
		Set(value As DateTime)
			workingPublisher.BeganPublishing = value.Date
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property CeasedPublishing As DateTime?
		Get
			Return workingPublisher.CeasedPublishing
		End Get
		Set(value As DateTime?)
			If value IsNot Nothing Then
				workingPublisher.CeasedPublishing = CType(value, Date).Date
				db.SaveChanges()
				NotifyPropertyChanged()
			End If
		End Set
	End Property

	Public Property MovedIn As Boolean
		Get
			Return workingPublisher.IsMoveIn
		End Get
		Set(value As Boolean)
			workingPublisher.IsMoveIn = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property MovedOut As Boolean
		Get
			Return workingPublisher.IsMoveOut
		End Get
		Set(value As Boolean)
			workingPublisher.IsMoveOut = value
			db.SaveChanges()
			NotifyPropertyChanged()
		End Set
	End Property

	Public Property GroupID As Long?
		Get
			Return workingPublisher.GroupID
		End Get
		Set(value As Long?)
			If db.Groups.Where(Function(x) x.GroupID = value).Count > 0 Then
				workingPublisher.GroupID = value
			End If
		End Set
	End Property

	Public ReadOnly Property GroupOverseerID As Long
		Get
			If workingPublisher.Group IsNot Nothing Then
				Return workingPublisher.Group.GroupOverseerID
			Else
				Return workingPublisher.PublisherID
			End If
		End Get
	End Property

	Public ReadOnly Property GroupAssistantID As Long
		Get
			If workingPublisher.Group IsNot Nothing Then
				Return workingPublisher.Group.GroupAssistantID
			Else
				Return workingPublisher.PublisherID
			End If
		End Get
	End Property

	Public Sub New(Optional PublisherID As Long? = Nothing)

		If PublisherID Is Nothing Then
			workingPublisher = New PublisherData
			'workingPublisher.PublisherID = PublisherID
			'workingPublisher = db.PublisherDatas.CreateObject()
			workingPublisher.FullName = "New Publisher"
			db.PublisherDatas.AddObject(workingPublisher)
			db.SaveChanges()
		Else
			workingPublisher = db.PublisherDatas.SingleOrDefault(Function(x) x.PublisherID = PublisherID)

			If workingPublisher Is Nothing Then
				Throw New ArgumentOutOfRangeException("A Publisher with that ID doesn't exist.")
			End If
		End If

	End Sub

	Public ReadOnly Property AllMobileProviders As MyListItem()
		Get
			Dim returnList As New List(Of MyListItem)

			returnList.Add(New MyListItem("Unknown", Nothing))

			For Each provider In db.MobileProviders
				returnList.Add(New MyListItem(provider.ServiceProvider, provider.ProviderID))
			Next

			Return returnList.ToArray
		End Get
	End Property

#Region "IDisposable Support"
	Private disposedValue As Boolean ' To detect redundant calls

	' IDisposable
	Protected Overridable Sub Dispose(disposing As Boolean)
		If Not Me.disposedValue Then
			If disposing Then
				' TODO: dispose managed state (managed objects).
				db.Dispose()
			End If

			' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
			' TODO: set large fields to null.
		End If
		Me.disposedValue = True
	End Sub

	' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
	'Protected Overrides Sub Finalize()
	'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
	'    Dispose(False)
	'    MyBase.Finalize()
	'End Sub

	' This code added by Visual Basic to correctly implement the disposable pattern.
	Public Sub Dispose() Implements IDisposable.Dispose
		' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
		Dispose(True)
		GC.SuppressFinalize(Me)
	End Sub
#End Region

End Class
